
/*
LED1:  PD13  LED2:PD14 LED3:PD15
*/

#include "board.h"
#include "drv_gpio.h"
#include <rtthread.h>
#include <stdlib.h>

#include "led.h"
#include "common.h"



/* global vars */
unsigned int g_uiLedTimer;
unsigned int g_uiLedState;
unsigned char g_ucLedTwinkState = 0;
//static struct rt_thread LedThread;
//static char LedThread_stack[256];

/**************************************************************************************************
Function Name : Led_Light
Description   : Led light process
Input         : None
Output        : None
Return        : None
Other         : None
**************************************************************************************************/
void Led_Light(void)
{
    switch(g_uiLedState)
    {
    case RED_LED_LIGHT:
        rt_pin_write(RED_LED_PIN, PIN_HIGH);
        rt_pin_write(GREEN_LED_PIN, PIN_LOW);
        rt_pin_write(YELLOW_LED_PIN, PIN_LOW);
        break;
    case GREEN_LED_LIGHT:
        rt_pin_write(GREEN_LED_PIN, PIN_HIGH);
        rt_pin_write(RED_LED_PIN, PIN_LOW);
        rt_pin_write(YELLOW_LED_PIN, PIN_LOW);
        break;
    case YELLOW_LED_LIGHT:
        rt_pin_write(YELLOW_LED_PIN, PIN_HIGH);
        rt_pin_write(RED_LED_PIN, PIN_LOW);
        rt_pin_write(GREEN_LED_PIN, PIN_LOW);
        break;
    default:
        break;
    }

    return;
}

/**************************************************************************************************
Function Name : Led_StartTwinkle
Description   : Start twinkle for led
Input         : None
Output        : None
Return        : None
Other         : None
**************************************************************************************************/
void Led_StartTwinkle(void)
{
    switch(g_uiLedState)
    {
    case RED_LED_START_TWINKLE:
        rt_pin_write(RED_LED_PIN, PIN_HIGH);
        rt_pin_write(GREEN_LED_PIN, PIN_LOW);
        rt_pin_write(YELLOW_LED_PIN, PIN_LOW);
        g_ucLedTwinkState = 0;
        g_uiLedState = RED_LED_TWINKLE;
        g_uiLedTimer = rt_tick_get();
        break;
    case GREEN_LED_START_TWINKLE:
        rt_pin_write(GREEN_LED_PIN, PIN_HIGH);
        rt_pin_write(RED_LED_PIN, PIN_LOW);
        rt_pin_write(YELLOW_LED_PIN, PIN_LOW);
        g_ucLedTwinkState = 0;
        g_uiLedState = GREEN_LED_TWINKLE;
        g_uiLedTimer = rt_tick_get();
        break;
    case YELLOW_LED_START_TWINKLE:
        rt_pin_write(YELLOW_LED_PIN, PIN_HIGH);
        rt_pin_write(RED_LED_PIN, PIN_LOW);
        rt_pin_write(GREEN_LED_PIN, PIN_LOW);
        g_ucLedTwinkState = 0;
        g_uiLedState = YELLOW_LED_TWINKLE;
        g_uiLedTimer = rt_tick_get();
        break;
    default:
        break;
    }

    return;
}


/**************************************************************************************************
Function Name : Led_Twinkle
Description   : Led twinkle process
Input         : None
Output        : None
Return        : None
Other         : None
**************************************************************************************************/
void Led_Twinkle(void)
{
    switch(g_uiLedState)
    {
    case RED_LED_TWINKLE:
        if ( 0 == g_ucLedTwinkState )
        {
            if ( 1 == COMMON_GetTimerInterval(g_uiLedTimer, 500) )
            {
                rt_pin_write(RED_LED_PIN, PIN_LOW);
                g_uiLedTimer = rt_tick_get();
                g_ucLedTwinkState = 1;
            }
        }
        else if ( 1 == g_ucLedTwinkState )
        {
            if ( 1 == COMMON_GetTimerInterval(g_uiLedTimer, 500) )
            {
                rt_pin_write(RED_LED_PIN, PIN_HIGH);
                g_uiLedTimer = rt_tick_get();
                g_ucLedTwinkState = 0;
            }
        }
        break;
    case GREEN_LED_TWINKLE:
        if ( 0 == g_ucLedTwinkState )
        {
            if ( 1 == COMMON_GetTimerInterval(g_uiLedTimer, 500) )
            {
                rt_pin_write(GREEN_LED_PIN, PIN_LOW);
                g_uiLedTimer = rt_tick_get();
                g_ucLedTwinkState = 1;
            }
        }
        else if ( 1 == g_ucLedTwinkState )
        {
            if ( 1 == COMMON_GetTimerInterval(g_uiLedTimer, 500) )
            {
                rt_pin_write(GREEN_LED_PIN, PIN_HIGH);
                g_uiLedTimer = rt_tick_get();
                g_ucLedTwinkState = 0;
            }
        }
        break;
    case YELLOW_LED_TWINKLE:
        if ( 0 == g_ucLedTwinkState )
        {
            if ( 1 == COMMON_GetTimerInterval(g_uiLedTimer, 500) )
            {
                rt_pin_write(YELLOW_LED_PIN, PIN_LOW);
                g_uiLedTimer = rt_tick_get();
                g_ucLedTwinkState = 1;
            }
        }
        else if ( 1 == g_ucLedTwinkState )
        {
            if ( 1 == COMMON_GetTimerInterval(g_uiLedTimer, 500) )
            {
                rt_pin_write(YELLOW_LED_PIN, PIN_HIGH);
                g_uiLedTimer = rt_tick_get();
                g_ucLedTwinkState = 0;
            }
        }
        break;
    default:
        break;
    }

    return;
}

#if 0
/**************************************************************************************************
Function Name : Led_task_thread_entry
Description   : Led thread process
Input         : None
Output        : None
Return        : None
Other         : None
**************************************************************************************************/
void Led_task_thread_entry(void *parameter)
{
    while(1)
    {
        Led_Light();
        Led_StartTwinkle();
        Led_Twinkle();

        rt_thread_mdelay(100);
    }
}
#endif

/**************************************************************************************************
Function Name : EC200X_PowerOn
Description   : Power on ec200x
Input         : None
Output        : None
Return        : None
Other         : None
**************************************************************************************************/
void Led_Init(void)
{
    //rt_err_t res;

    rt_pin_mode(RED_LED_PIN, PIN_MODE_OUTPUT);
    rt_pin_mode(GREEN_LED_PIN, PIN_MODE_OUTPUT);
    rt_pin_mode(YELLOW_LED_PIN, PIN_MODE_OUTPUT);
    rt_pin_mode(SYSTEM_LED_PIN, PIN_MODE_OUTPUT);

    /* default yellow light */
    rt_pin_write(RED_LED_PIN, PIN_LOW);
    rt_pin_write(GREEN_LED_PIN, PIN_LOW);
    rt_pin_write(YELLOW_LED_PIN, PIN_HIGH);

    g_uiLedTimer = rt_tick_get();
    g_uiLedState = YELLOW_LED_LIGHT;

#if 0
    res = rt_thread_init(&LedThread, "led", Led_task_thread_entry, NULL, &LedThread_stack[0], sizeof(LedThread_stack), LED_THREAD_PRIORITY, 20);
    if ( RT_EOK != res )
    {
        rt_kprintf("Create Led thread Failed\n");
    }
    else
    {
        rt_thread_startup(&LedThread);
    }
#endif
}

/**************************************************************************************************
Function Name : Led_TaskProc
Description   : Led task process
Input         : None
Output        : None
Return        : None
Other         : None
**************************************************************************************************/
void Led_TaskProc(void)
{
    Led_Light();
    Led_StartTwinkle();
    Led_Twinkle();
}

/**************************************************************************************************
Function Name : Led_MoveState
Description   : Move LED process state
Input         : None
Output        : None
Return        : None
Other         : None
**************************************************************************************************/
void Led_MoveState(unsigned int uiLedState)
{
    if ( RED_LED_LIGHT == uiLedState )
    {
        if ( RED_LED_LIGHT == g_uiLedState )
        {
            return;
        }
        else
        {
            g_uiLedState = uiLedState;
        }
    }
    else if ( GREEN_LED_LIGHT == uiLedState )
    {
        if ( GREEN_LED_LIGHT == g_uiLedState )
        {
            return;
        }
        else
        {
            g_uiLedState = uiLedState;
        }
    }
    else if ( YELLOW_LED_LIGHT == uiLedState )
    {
        if ( YELLOW_LED_LIGHT == g_uiLedState )
        {
            return;
        }
        else
        {
            g_uiLedState = uiLedState;
        }
    }
    else if ( RED_LED_START_TWINKLE == uiLedState )
    {
        if ( (RED_LED_START_TWINKLE == g_uiLedState) || (RED_LED_TWINKLE == g_uiLedState) )
        {
            return;
        }
        else
        {
            g_uiLedState = uiLedState;
        }
    }
    else if ( GREEN_LED_START_TWINKLE == uiLedState )
    {
        if ( (GREEN_LED_START_TWINKLE == g_uiLedState) || (GREEN_LED_TWINKLE == g_uiLedState) )
        {
            return;
        }
        else
        {
            g_uiLedState = uiLedState;
        }
    }
    else if ( YELLOW_LED_START_TWINKLE == uiLedState )
    {
        if ( (YELLOW_LED_START_TWINKLE == g_uiLedState) || (YELLOW_LED_TWINKLE == g_uiLedState) )
        {
            return;
        }
        else
        {
            g_uiLedState = uiLedState;
        }
    }
}

void ledstate(int argc, char **argv)
{
    rt_kprintf("Led state:%d\n", g_uiLedState);
}
MSH_CMD_EXPORT(ledstate , Print led state);

